package com.li.client.util;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;

/**
 * abstract base class for GWT call back.
 * 
 * @param <T>
 *            the expected Results Type.
 */
public abstract class GwtCallBack<T> implements AsyncCallback<T>
{
	//CHECKSTYLE:OFF
	public static final String ALERT ="Alert!";
	public static final String UNAUTHORIZED = "401";
	private boolean disableMask;
	//CHECKSTYLE:ON
	
	public GwtCallBack(boolean disableMask)
	{
		this.disableMask = disableMask;
		if (!disableMask)
		{
			BusyMask.INSTANCE.show(null);
		}
	}

	public GwtCallBack()
	{
		BusyMask.INSTANCE.show(null);
	}

	public GwtCallBack(String action)
	{
		BusyMask.INSTANCE.show(action);
	}

	public void onFailure(Throwable e)
	{
		SC.say(e.getMessage());
		
		cleanup();
	}
	
	public void onSuccess(T result)
	{
		try
		{
			onResult(result);
		}
		catch (Exception e)
		{
			SC.say(e.getMessage());
			
			cleanup();
		}
		cleanup();
	}

	protected abstract void onResult(T result);

	protected void cleanup()
	{
		if (!disableMask)
		{
			BusyMask.INSTANCE.hide();
		}
	}
}
